<template>
  <view class="update-page">

    <!-- 身高 -->
    <view class="form-group">
      <text class="label bold">身高（cm）</text>
      <input class="input" type="number" v-model="height" placeholder="输入身高信息" />
    </view>

    <!-- 体重 -->
    <view class="form-group">
      <text class="label bold">体重（kg）</text>
      <input class="input" type="number" v-model="weight" placeholder="请输入体重信息" />
    </view>

    <!-- 血压 -->
    <view class="form-group">
      <text class="label bold">血压（mmHg）</text>
      <input class="input" v-model="bp" placeholder="血压信息" />
    </view>

    <!-- 血糖 -->
    <view class="form-group">
      <text class="label bold">血糖（mmol/L）</text>
      <input class="input" v-model="sugar" placeholder="血糖信息" />
    </view>

    <!-- 确认按钮 -->
    <view class="btn-wrap">
      <button class="submit-btn" @click="onSubmit">确认更新</button>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref } from "vue";
// 引入接口
import { updateHealthData } from "@/https/update";
// 引入类型
import type { UpdateHealthData } from "@/types/update";
// 定义变量
// 4. 引入 Pinia 健康数据 store
import { useHealthStore } from '@/store/health/health'

const height = ref('')
const weight = ref('')
const bp = ref('')
const sugar = ref('')

// 6. 获取健康数据的全局 store 实例
const healthStore = useHealthStore()

const onBack = () => {
  uni.navigateBack();
}
const onSubmit = async () => {
  // 将需要发送给后端的数据封装成对象
  const params: UpdateHealthData = {
    height: height.value,
    weight: weight.value,
    blood_ressure: bp.value,
    blood_sugar: sugar.value,
  }
  console.log(params)
  // try {
  //   const res = await updateHealthData(params)
  //   if (res.code === 200) {

  //     uni.showToast({ title: '更新成功', icon: 'success' })

  //     console.log(res);
  //   } else {
  //     uni.showToast({ title: res.msg || '更新失败', icon: 'none' })
  //   }
  // } catch (error) {
  //   console.error(error)
  // }

  // 7.3 更新 Pinia 全局健康数据
  healthStore.setHealthData(params)
  uni.navigateTo({ url: '/pages/profile/profile' })
}
</script>

<style scoped>
.update-page {
  background: #fff;
  min-height: 100vh;
  padding: 0 0 40rpx 0;
}
.header {
  display: flex;
  align-items: center;
  height: 100rpx;
  background: #fff;
  box-shadow: 0 8rpx 24rpx rgba(25, 195, 60, 0.08);
  padding: 0 32rpx;
  position: relative;
}
.back {
  font-size: 48rpx;
  color: #19c37c;
  font-weight: bold;
  margin-right: 24rpx;
  cursor: pointer;
}
.title {
  font-size: 36rpx;
  color: #19c37c;
  font-weight: bold;
}
.form-group {
  margin: 0 32rpx 0 32rpx;
  border-bottom: 2rpx solid #19c37c;
  padding: 32rpx 0 0 0;
}
.label {
  font-size: 30rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 8rpx;
  display: block;
}
.input {
  width: 100%;
  border: none;
  background: none;
  font-size: 28rpx;
  color: #333;
  margin: 8rpx 0 0 0;
  outline: none;
}
.btn-wrap {
  margin: 64rpx 32rpx 0 32rpx;
}
.submit-btn {
  width: 100%;
  height: 88rpx;
  background: #19c37c;
  color: #fff;
  font-size: 36rpx;
  font-weight: bold;
  border-radius: 16rpx;
  box-shadow: 0 8rpx 24rpx rgba(25, 195, 60, 0.18);
  border: none;
}
</style>
